//引入工具类
const util = require('../../utils/util');
// 加载配置文件
const config = require('../../utils/config');

const app = getApp()

Page({
  data: {
    listData: [],
    selectId: [],
    page: 1,
    searVal: '',
    lastPage: false,
    pageTitle: '',
    reload: false,
    modalShow: false,
    type: '',
    modalAction: [{
        name: '取消'
      },
      {
        name: '删除',
        color: '#ed3f14',
        loading: false
      }
    ]
  },
  changeSearVal(e) {
    this.setData({
      searVal: e.detail
    })
  },
  onSearch() {
    this.resetLoadData();
  },
  changeSelect(e) {
    let _index = +e.target.dataset.index;
    let dataList = JSON.parse(JSON.stringify(this.data.listData))

    dataList[_index].actived = dataList[_index].actived == '' ? 'actived' : ''

    if (!!dataList[_index].actived) {
      this.data.selectId.push(dataList[_index].id)
    } else {
      var delIndex = this.data.selectId.indexOf(dataList[_index].id)
      if (delIndex !== -1) {
        this.data.selectId.splice(delIndex, 1)
      }
    }

    console.log(this.data.selectId)

    this.setData({
      listData: dataList
    });
  },
  delListData(e) {
    if (!this.data.listData.length) {
      wx.showToast({
        title: '暂无数据',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    let type = e.currentTarget.dataset.type
    this.setData({
      type: type
    })

    if (!this.data.selectId.length && this.data.type !== "setReadAll") {
      wx.showToast({
        title: '请选择数据',
        icon: 'none',
        duration: 1000
      })
      return;
    }

    this.setData({
      modalShow: true
    })
  },
  handleSetRead(e) {
    if (!this.data.listData.length) {
      wx.showToast({
        title: '暂无数据',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    let _this = this;
    let type = e.currentTarget.dataset.type
    this.setData({
      type: type
    })

    let apiUrl = 'msg/setRead';
    let params = {
      ids: this.data.selectId.join(",")
    }
    if (this.data.type == "setReadAll") {
      params.ids = "";
    }
    if (!this.data.selectId.length && this.data.type !== "setReadAll") {
      wx.showToast({
        title: '请选择数据',
        icon: 'none',
        duration: 1000
      })
      return;
    }

    wx.showLoading({
      title: "正在处理..."
    })
    app.ajax({
      url: `${config.httpUrl}${apiUrl}`,
      method: 'post',
      data: params,
      callback: function(error, data) {
        wx.hideLoading()
        _this.resetLoadData();
      }
    })
  },
  handleDelClick({
    detail
  }) {
    if (detail.index === 0) {
      this.setData({
        modalShow: false
      });
    } else {
      const action = [...this.data.modalAction];
      action[1].loading = true;

      this.setData({
        modalAction: action
      });


      let _this = this;

      let apiUrl = '';
      let params = {
        ids: this.data.selectId.join(",")
      }
      if (this.data.pageTitle == "现场执法") {
        apiUrl = 'law/deleteLaw'
      } else if (this.data.pageTitle == "消息中心") {
        apiUrl = 'msg/delete'
      }
      wx.showLoading({
        title: '正在删除...'
      })
      app.ajax({
        url: `${config.httpUrl}${apiUrl}`,
        method: 'post',
        data: params,
        callback: function(error, data) {
          action[1].loading = false;
          _this.setData({
            modalShow: false,
            modalAction: action
          });
          wx.hideLoading()
          _this.resetLoadData();
        }
      })
    }
  },
  getListData() {
    wx.showLoading({
      title: '加载中...'
    })
    let _this = this;
    let apiUrl = ''
    if (this.data.pageTitle == "现场执法") {
      apiUrl = 'law/findList'
    } else if (this.data.pageTitle == "消息中心") {
      apiUrl = 'msg/findList'
    }
    app.ajax({
      url: `${config.httpUrl}${apiUrl}`,
      method: 'get',
      data: {
        keyword: this.data.searVal,
        page: this.data.page,
        rows: 20
      },
      callback: function(error, data) {
        wx.hideLoading();
        let dataList = data.data.list.map((item) => {
          return {
            content: item.content,
            create_time: item.create_time,
            id: item.id,
            msgId: item.msgId || '',
            title: item.title,
            status: item.status,
            statusName: item.statusName,
            actived: ''
          }
        })
        _this.setData({
          page: _this.data.page + 1,
          lastPage: data.data.lastPage,
          listData: _this.data.listData.concat(dataList)
        });
      }
    })
  },
  goDetail(e) {
    let _id = e.currentTarget.dataset.id || '';

    let url = ''
    if (this.data.pageTitle == "现场执法") {
      url = `/pages/scene-detail/scene-detail?id=${_id}`
    } else if (this.data.pageTitle == "消息中心") {
      // 消息中心查看详情用的是msgId
      _id = e.currentTarget.dataset.msgid;
      let curData = this.data.listData.map((item) => {
        if (item.msgId == _id) {
          item.status = 1
          item.statusName = "已读"
        }
        return item
      })
      this.setData({
        listData: curData
      })
      url = `/pages/msg-detail/msg-detail?id=${_id}&name=消息中心`
    }

    wx.navigateTo({
      url
    });
  },

  onLoad: function(options) {
    var _this = this;
    _this.setData({
      pageTitle: options.name
    })
    wx.setNavigationBarTitle({
      title: _this.data.pageTitle
    })

    this.resetLoadData()
  },
  onShow: function() {
    if (this.data.reload) {
      this.resetLoadData()
    }

    let pages = getCurrentPages();
    let curPage = pages[pages.length - 1];
    app.checkBind(curPage.route);
  },

  resetLoadData() {
    this.setData({
      page: 1,
      listData: [],
      selectId: []
    })
    this.getListData();
  },

  onPullDownRefresh: function() {
    this.setData({
      searVal: ''
    })
    this.resetLoadData();
    wx.stopPullDownRefresh()
  },

  onReachBottom: function() {
    if (!this.data.lastPage) {
      this.getListData();
    }

  }
})